/* Copyright (C) 2014-2016 Dan Chapman <dpniel@ubuntu.com>

   This file is part of Dekko email client for Ubuntu Devices/

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of
   the License or (at your option) version 3

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "NotificationWorker.h"
#include <QFile>
#include <QDateTime>
#include "Common/Paths.h"
#include "app/Accounts/AccountUtils.h"

namespace Dekko
{
namespace Notifications
{
NotificationWorker::NotificationWorker(QObject *parent, Accounts::Account *account) :
    QObject(parent), m_account(account), m_watcher(0)
{
    if (!static_cast<Dekko::Settings::ImapSettings *>(
                m_account->imapSettings())->server().isEmpty()) {
        // So for each account we need a mailbox watcher
        m_watcher = new MailboxWatcher(this);
        m_watcher->setStartDateTime(QDateTime::currentDateTime());
        m_watcher->setMailbox(QStringLiteral("INBOX")); // TODO: we should make this configurable and allow more than one mailbox per account to be watched.
        connect(m_watcher, &MailboxWatcher::newMessages, this, &NotificationWorker::newMessage);
        m_watcher->setAccount(account);
        connect(m_account,
                SIGNAL(checkSslPolicy()),
                this,
                SLOT(setSslPolicy()));
        connect(account->notificationModel(),
                SIGNAL(connectionStateChanged(QObject*, Imap::ConnectionState)),
                m_watcher,
                SLOT(onConnectionStateChanged(QObject*,Imap::ConnectionState)));
        connect(m_watcher, &MailboxWatcher::messageNowMarkedRead, this, &NotificationWorker::messageNowMarkedRead);
        connect(m_watcher, SIGNAL(countChanged()), this, SLOT(updateUnreadCount()));
        connect(m_account->notificationModel(),
                SIGNAL(imapError(QString)),
                this,
                SLOT(reconnect(QString)));
        connect(m_account->notificationModel(),
                SIGNAL(networkError(QString)),
                this,
                SLOT(reconnect(QString)));
        connect(m_account->notificationModel(),
                SIGNAL(alertReceived(QString)),
                this,
                SLOT(reconnect(QString)));
    }
    // open the connection
    if (!m_account->notificationModel()->isNetworkOnline()) {
        m_account->notificationNetworkWatcher()->setNetworkOnline();
        m_account->notificationModel()->reloadMailboxList();
    }
}


void NotificationWorker::setNetworkPolicy(const bool online)
{
    if (online) {
        m_account->notificationNetworkWatcher()->setNetworkOnline();
    } else {
        m_account->notificationNetworkWatcher()->setNetworkOffline();
    }
}

void NotificationWorker::updateStartTime(const QDateTime time)
{
    qDebug() << "Updating start time: " << time;
    m_watcher->setStartDateTime(time);
}


//void NotificationWorker::setSslPolicy()
//{
//    Imap::Mailbox::Model *model = static_cast<Imap::Mailbox::Model *>(sender());
//    Q_FOREACH(Accounts::Account *account, m_accounts) {
//        if (static_cast<Imap::Mailbox::Model *>(account->imapModel())->imapUser() == model->imapUser()) {
//            account->setSslPolicy(true); // TODO: find a way for the user to accept this
//            return;
//        }
//    }
//}

//void NotificationWorker::updateUnreadCount()
//{
//    int count = 0;
//    Q_FOREACH(Accounts::Account *account, m_accounts) {
//        count += static_cast<Imap::Mailbox::MsgListModel *>(account->msgListModel())->unreadCount();
//    }
//    emit unreadCountChanged(count);
//}

}
}
